package com.seu.utils;

import com.seu.consumer.AlarmPulsarConsumer;
import com.seu.producer.TestPulsarProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationRunner implements ApplicationRunner {
    private static final Logger log = LoggerFactory.getLogger(MyApplicationRunner.class);

    /**
     * 通过实现ApplicationRunner接口，在启动SpringBootApplication后执行该类的run方法
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        TestPulsarProducer bean = SpringUtils.getBean(TestPulsarProducer.class);

        // 开始消费
        new Thread(()->{
            AlarmPulsarConsumer alarmPulsarConsumer = SpringUtils.getBean(AlarmPulsarConsumer.class);
            try{
                alarmPulsarConsumer.fetch();
            }catch(Exception e){
                log.error("消费Pulsar数据异常，停止Pulsar连接：", e);
                alarmPulsarConsumer.close();
            }
        }).start();

        bean.sendMsg("java","[LZHTest]hello world, we are lonsun, we will use pulsar[LZHTest]");
    }
}
